<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请输入类型" clearable >
          <el-option
              v-for="dict in dict.type.company_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
        </el-select>
      </el-form-item>

      <el-form-item label="机构代码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入组织机构代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:company:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:company:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:company:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:company:export']"
          >导出</el-button
        >
      </el-col>
      <audio
        controls="controls"
        hidden
        src="../voice/zhendong.mp3"
        ref="audio1"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/tips.mp3"
        ref="audio2"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/delete.mp3"
        ref="audio3"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/Download.mp3"
        ref="audio4"
      ></audio>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="companyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
      type="index" 
        label="序号"
        width="50px"
      ></el-table-column>
      <el-table-column
        label="公司名称"
        align="center"
        prop="companyName"
        width="100px"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="组织机构代码"
        align="center"
        prop="code"
        width="120px"
      />
      <el-table-column label="添加时间" align="center" prop="addTime" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="公司地址"
        align="center"
        prop="address"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="纳税人识别号"
        align="center"
        prop="number"
        width="120px"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="公司邮箱"
        align="center"
        prop="email"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="公司电话"
        align="center"
        prop="phone"
        width="100px"
      >
        <template slot-scope="scope">
          {{
            scope.row.phone != null
              ? scope.row.phone.replace(/^(.{3}).*(.{4})$/, "$1****$2")
              : null
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="公司类型"
        align="center"
        prop="dict_label"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column label="性质" align="center" prop="quality" /> -->
      <el-table-column label="经营期限" align="center" prop="term" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:company:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:company:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLookOver(scope.row)"
            v-hasPermi="['system:company:query']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="机构代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入组织机构代码" v-bind:disabled="disabled"/>
        </el-form-item>

        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="识别号" prop="number">
          <el-input v-model="form.number" placeholder="请输入纳税人识别号" v-bind:disabled="disabled"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="电话" prop="phone" >
          <el-input v-model="form.phone"  placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select
            v-model="form.type"
            placeholder="类型"
            clearable
            filterable
          >
            <el-option
              v-for="dict in dict.type.company_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="经营期限" prop="term">
          <el-input v-model="form.term" placeholder="请输入生产经营期限" v-bind:disabled="disabled"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看公司弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="lookOver"
      width="500px"
      class="maskpopup"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="22">
            <el-form-item label="公司名称：">{{
              form.companyName
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="组织机构代码：">{{
              form.code
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="添加时间：">{{
              form.addTime
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="公司地址：">{{
              form.address
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="纳税人识别号：">{{
              form.number
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="公司邮箱：">{{
              form.email
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="公司电话：">{{ form.phone }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="公司类型：">{{
              form.dict_label
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="经营期限：">{{
              form.term
            }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompany,
  getCompany,
  delCompany,
  addCompany,
  updateCompany,
} from "@/api/system/company";

export default {
  name: "Company",
  dicts: ["company_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      disabled:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      lookOver: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId:null,
        companyName: null,
        code: null,
        addTime: null,
        address: null,
        number: null,
        email: null,
        dict_label: null,
        phone: null,
        type: null,
        term: null,
        fakeDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" },
          { pattern: /\S/, message: "公司名称不能为空", trigger: "blur" },
          { pattern: /^[^ ]+$/, message: "不能包含空格", trigger: "blur" },
          { pattern: /^(?!.*[0-9])/, message: "不能包含数字", trigger: "blur" },
        ],
        code: [
          {
            required: true,
            message: "请输入机构代码",
            trigger: "change",
          },
          {
            pattern: /[a-zA-Z0-9]{8}-[a-zA-Z0-9]/,
            message: "组织机构代码格式错误,请按照营业执照输入",
            trigger: "change",
          },
          { pattern: /^[^ ]+$/, message: "不能包含空格", trigger: "change" },

          { pattern: /\S/, message: "机构代码不能为空", trigger: "blur" },
        ],
        type: [
          {
            required: true,
            message: "类型不能为空",
            trigger: "change",
          },
        ],

        address: [
          { required: true, message: "参数名称不能为空", trigger: "blur" },
          { pattern: /^[^ ]+$/, message: "不能包含空格", trigger: "blur" },
        ],

        number: [
          {
            required: true,
            message: "请输入纳税人识别号",
            trigger: "blur",
          },
          {
            pattern: /^[A-Z0-9]{15}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/,
            message: "纳税人识别号格式错误",
            trigger: "blur",
          },
          { pattern: /^[^ ]+$/, message: "不能包含空格", trigger: "blur" },
        ],

        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            pattern:
              /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: "邮箱格式错误",
            trigger: "change",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入电话",
            trigger: "blur",
          },
          {
            pattern: /^[1]+[3,4,5,6,7,8,9]+\d{9}$/,
            message: "电话格式错误",
            trigger: "blur",
          },
        ],
       
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公司列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then((response) => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyId: null,
        companyName: null,
        code: null,
        updateTime: null,
        addTime: null,
        address: null,
        number: null,
        email: null,
        phone: null,
        type: null,
        quality: null,
        term: null,
        fakeDelete: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.companyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司";
   this.disabled=false
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const companyId = row.companyId || this.ids;
      getCompany(companyId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司";
        this.disabled = true
      });
    },
    /** 查看详情 */
    handleLookOver(row) {
      this.$refs.audio2.currentTime = 0;
      this.$refs.audio2.play();
      getCompany(row.companyId).then((response) => {
        this.form = response.data;
        this.lookOver = true;
        this.title = "查看公司详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        console.log(this.form);
        if (valid) {
          if (this.form.companyId != null) {
            updateCompany(this.form).then((response) => {
              this.$modal.msgSuccess("该公司数据已成功修改");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then((response) => {
              console.log(this.form);
              this.$modal.msgSuccess("新增公司成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$refs.audio3.currentTime = 0;
      this.$refs.audio3.play();
      const companyIds = row.companyId || this.ids;
      this.$modal
        .confirm('是否确认删除？')
        .then(function () {
          return delCompany(companyIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("该公司已成功删除");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$refs.audio4.currentTime = 0;
      this.$refs.audio4.play();
      this.download(
        "system/company/export",
        {
          ...this.queryParams,
        },
        `company_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
